//
//  TWVideoCollectionViewCell.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/17.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit
import SDWebImage

class TWVideoCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var bgImageView: UIImageView!
    
    var cellModel : TWItemModel?{
        didSet {
            guard let cellModel = cellModel else {
                return
            }
            let urlStr = cellModel.cover?.detail
            let url = URL(string: urlStr!)
            
            // 异步检查图片是否已经被缓存
            let image = SDImageCache.shared().imageFromMemoryCache(forKey: urlStr)
            if image == nil {
                bgImageView.sd_setImage(with: url)
            } else {
                bgImageView.image = image
            }
        }
    }
    @objc private func showImageAnimation() {
        UIView.animate(withDuration: 5, animations: {
            self.bgImageView.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
        }) { (_) in
            UIView.animate(withDuration: 5, animations: {
            self.bgImageView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            })
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        bgImageView.contentMode = .scaleAspectFill
        bgImageView.clipsToBounds = true
        let time : TimeInterval = 10
        let timer = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(showImageAnimation), userInfo: nil, repeats: true)
        RunLoop.main.add(timer, forMode: .commonModes)
    }
}
